package com.abo.carrent.recommend;

/**
 * 作用描述
 *
 * @Author: Eric
 */

import java.util.ArrayList;
import java.util.List;


public class UserSet {

    public List<User> users = new ArrayList<>();

    public UserSet() {
    }

    public User put(String userId) {
        return new User(userId);
    }

    // 根据索引获取集合中的用户对象
    public User getUser(int position) {
        return users.get(position);
    }

    // 根据用户id获取用户对象
    public User getUser(String userId) {
        for (User user : users) {
            if (user.userId.equals(userId)) {
                return user;
            }
        }
        return null;
    }


    public final class User {

        // 用户ID
        public String userId;

        // 推荐的信息集合
        public List<RInfo> list = new ArrayList<>();

        private User(String userId) {
            this.userId = userId;
        }

        // 传入推荐信息编号及收藏的数量
        public User set(Long id, Long num) {
            this.list.add(new RInfo(id,num));
            return this;
        }

        public void create() {
            users.add(this);
        }

        public RInfo find(Long id) {
            for (RInfo rInfo : list) {
                if (rInfo.id.equals(id)) {
                    return rInfo;
                }
            }
            return null;
        }

        @Override
        public String toString() {
            return "User{" +
                    "userId='" + userId + '\'' +
                    '}';
        }
    }


    public final class RInfo implements Comparable<RInfo> {
        // 推荐信息编号
        public Long id;
        // 推荐信息数量
        public Long num;

        public RInfo(Long id, Long num) {
            this.id = id;
            this.num = num;
        }

        @Override
        public String toString() {
            return "RInfo{" +
                    "id='" + id + '\'' +
                    ", num=" + num +
                    '}';
        }

        @Override
        public int compareTo(RInfo o) {
            return num > o.num ? -1 : 1;
        }
    }

}